package com.maltys.dp;

/**
 * 给定不同⾯额的硬币和⼀个总⾦额。写出函数来计算可以凑成总⾦额的硬币组合数。假设每⼀种⾯额的硬币有⽆限
 * 个。
 * 示例 1:
 * 输⼊: amount = 5, coins = [1, 2, 5]
 * 输出: 4
 * 解释: 有四种⽅式可以凑成总⾦额:
 * 5=5
 * 5=2+2+1
 * 5=2+1+1+1
 * 5=1+1+1+1+1
 * 示例 2:
 * 输⼊: amount = 3, coins = [2]
 * 输出: 0
 * 解释: 只⽤⾯额2的硬币不能凑成总⾦额3。
 * int main() {
 * test_CompletePack();
 * }
 * 示例 3:
 * 输⼊: amount = 10, coins = [10]
 * 输出: 1
 * 注意，你可以假设：
 * 0 <= amount (总⾦额) <= 5000
 * 1 <= coin (硬币⾯额) <= 5000
 * 硬币种类不超过 500 种
 * 结果符合 32 位符号整数
 */
public class ChangeExchange {
    public static void main(String[] args) {
        ChangeExchange exchange = new ChangeExchange();
        int changeCount = exchange.smallChangeCount(new int[]{2}, 3);
        System.out.println(changeCount);
    }

    public int smallChangeCount(int[] coins, int sum) {
        int[][] dp = new int[coins.length][sum + 1];
        for (int i = 0; i < coins.length; i++) {
            dp[i][0] = 1;
        }
        for (int i = 1; i <= sum; i++) {
            if (i % coins[0] == 0) {
                dp[0][i] = 1;
            }
        }
        for (int i = 1; i < coins.length; i++) {
            for (int j = 1; j <= sum; j++) {
                dp[i][j] += dp[i - 1][j];
                if (j >= coins[i]) {
                    dp[i][j] += dp[i][j - coins[i]];
                }
            }
        }
        return dp[coins.length - 1][sum];
    }
}
